package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lsq
 */
@Service
public class NursingLevelServiceImpl implements NursingLevelService {
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Override
    public List<NursingLevelVo> getAllNursingLevel() {
        return nursingLevelMapper.listAll();
    }

    @Override
    public void addNursingLevel(NursingLevelDto dto) {
        NursingLevel nursingLevel = BeanUtil.toBean(dto, NursingLevel.class);
        nursingLevelMapper.insert(nursingLevel);
    }

    @Override
    public PageResponse<NursingLevelVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<List<NursingLevel>> lists = nursingLevelMapper.listByPage(pageNum, pageSize, name, status);
        return PageResponse.of(lists,NursingLevelVo.class);
    }

    @Override
    public NursingLevelVo getById(Long id) {
        NursingLevel nursingLevel = nursingLevelMapper.getById(id);
        return BeanUtil.toBean(nursingLevel,NursingLevelVo.class);
        
    }

    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public void updateById(NursingLevelDto dto) {
        NursingLevel nursingLevel = BeanUtil.toBean(dto, NursingLevel.class);
        Integer count  = nursingLevelMapper.isBinding(nursingLevel.id);
        if (count>0){
            throw new BaseException(BasicEnum.NURSINGPLAN_BINDING_USER);
        }
        nursingLevelMapper.update(nursingLevel);
    }

    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public void deleteById(Long id) {
        Integer count  = nursingLevelMapper.isBinding(id);
        if (count>0){
            throw new BaseException(BasicEnum.NURSINGPLAN_BINDING_USER);
        }
        nursingLevelMapper.delete(id);

    }

    @Override
    public void updateStatus(Long id, Integer status) {
        nursingLevelMapper.updateStatus(id,status);
    }


}
